<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签必须放在最前面，任何其他内容都必须跟随其后！ -->
    <title>cart</title>
    <!-- Bootstrap样式 -->
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/cart.css">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="jquery/jquery-3.4.1.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

    <!--前端渲染-->
    <script src="js/jsrender.js"></script>

    <script src="js/Ymall.js"></script>
    <link rel="stylesheet" href="css/Ymall.css">
    <style>
        #copyRight-frame{
            position: fixed;
            bottom: 0;
            margin: auto;
        }
        .del {
            cursor: pointer;
        }
    </style>
</head>
<body>
<!-- navbar导航栏 -->
<nav class="navbar navbar-default">
    <div class="container-fluid" id="navbar">
        <div class="row clearfix">
            <div class="col-md-1 column">
            </div>
            <div class="col-md-10 column">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#"><img class="img-logo" src="img/logol.png" alt=""></a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li><a href="Ymall.html"><span class="span-font">首页</span><span class="sr-only">(current)</span></a></li>
                        <li><a href="Mall.html"><span class="span-font">购物商城</span> </a></li>
                        <li><a href="#"><span class="span-font">求购商城</span> </a></li>
                        <li><a href="#"><span class="span-font">购物车</span> </a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="span-font">反馈与意见</span> <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#"><span class="span1-font">反馈</span></a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#"><span class="span1-font">意见</span></a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="span-font">联系我们</span> <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#"><span class="span1-font">电话联系</span></a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="#"><span class="span1-font">邮箱联系</span></a></li>
                            </ul>
                        </li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <li id="login"><a href="Login.html"><span class="span-font">登录</span></a></li>
                        <li id="register"><a href="Register.html"><span class="span-font">注册</span></a></li>
                    </ul>
                    <form class="navbar-form navbar-right">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                        </div>
                        <!-- <button type="submit" class="btn btn-default"><img class="img-search" src="img/" alt=""></button> -->
                        <button type="submit" class="btn btn-default"><img class="img-search" src="img/search_icon.png" alt=""></button>
                    </form>
                </div>
            </div>
            <div class="col-md-1 column">
            </div>
        </div>
    </div>
</nav>
<!--购物车主体部分-->
<div class="container-fluid">
    <div class="row clearfix">
        <div class="col-md-2 column">
        </div>
        <div class="col-md-8 column">
            <div id="label" style="height: 50px">
                <div class="cart" style="float: left; font-size: 20px">购物车</div>
                <div class="continue" style="float: right; font-size: 20px;"><a href="Mall.html">继续购物</a></div>
            </div>
            <div id="cart-bar">
                <ul class="cart-bar">
                    <li class="cart-li">商品</li>
                    <li class="cart-li">单价</li>
                    <li class="cart-li">数量</li>
                    <li id="tdel-dis" class="cart-li">小计</li>
                    <li class="cart-li tdel">操作</li>
                </ul>
            </div>
            <div id="cart-goods">
                <div class="cart-div" id="cart-div0">

                </div>
                <div class="cart-div" id="cart-div1">
                    <!--                    <input type="checkbox" name="checkbox" value="goods1"/>-->
                    <!--                    <img src="img/goods2.jpg" alt="" style="width: 120px; height: 120px; display: inline-block; vertical-align: bottom;">-->
                    <!--                    <div style="display: inline-block; margin-left: 3%">-->
                    <!--                        <p>精品键盘</p>-->
                    <!--                        <p style="color: #c4c4c4; font-size: 13px">颜色分类：</p>-->
                    <!--                        <p style="color: #c4c4c4; font-size: 13px">墨黑色</p>-->
                    <!--                    </div>-->
                    <!--                    <p style="display: inline-block; margin-left: 9%;">￥100.00</p>-->
                    <!--                    <div style="display: inline-block; margin-left: 11.5%">-->
                    <!--                        <span class="sub" id="sub2"><img src="img/sub.jpg" alt="" ></span>&nbsp;<span><input type="text" value="1" class="val1" style="outline: none;"></span>-->
                    <!--                        <span class="add" id="add2"><img src="img/add.jpg" alt="" ></span>-->
                    <!--                    </div>-->
                    <!--                    <div style="display: inline-block; margin-left: 10%" class="price" id="price2">-->
                    <!--                    </div>-->
                    <!--                    <p style="display: inline-block; margin-left: 13%;" class="del">删除</p>-->
                </div>
                <div class="cart-div" id="cart-div2">
                    <!--                    <input type="checkbox" name="checkbox" value="goods1"/>-->
                    <!--                    <img src="img/goods3.jpg" alt="" style="width: 120px; height: 120px; display: inline-block; vertical-align: bottom;">-->
                    <!--                    <div style="display: inline-block; margin-left: 3%">-->
                    <!--                        <p>精品键盘</p>-->
                    <!--                        <p style="color: #c4c4c4; font-size: 13px">颜色分类：</p>-->
                    <!--                        <p style="color: #c4c4c4; font-size: 13px">墨黑色</p>-->
                    <!--                    </div>-->
                    <!--                    <p style="display: inline-block; margin-left: 9%;">￥100.00</p>-->
                    <!--                    <div style="display: inline-block; margin-left: 11.5%">-->
                    <!--                        <span class="sub" id="sub3"><img src="img/sub.jpg" alt="" ></span>&nbsp;-->
                    <!--                        <input type="text" value="1" class="val1" style="outline: none;">-->
                    <!--                        <span class="add" id="add3"><img src="img/add.jpg" alt="" ></span>-->
                    <!--                    </div>-->
                    <!--                    <div style="display: inline-block; margin-left: 10%" class="price" id="price3">-->
                    <!--                    </div>-->
                    <!--                    <p style="display: inline-block; margin-left: 13%;" class="del">删除</p>-->
                </div>
                <div class="cart-div" id="cart-div3">
                </div>
                <div class="cart-div" id="cart-div4">

                </div>
                <div class="cart-div" id="cart-div5">
                </div>
                <div class="cart-div" id="cart-div6">
                </div>
                <div class="cart-div" id="cart-div7">
                </div>
                <div class="cart-div" id="cart-div8">
                </div>
                <div class="cart-div" id="cart-div9">
                </div>
                <div class="cart-div" id="cart-div10">
                </div>
                <div class="cart-div" id="cart-div11">
                </div>
                <div class="cart-div" id="cart-div12">
                </div>
            </div>
            <div style="height: 50px">
                <div id="tdel-show" style="float: right; font-size: 20px;">
                    <button class="btn btn-default" href="Mall.html" data-loading-text="Loading..." style="text-decoration: none" onclick="addOrder()">提交订单</button>
                </div>
            </div>
        </div>
        <div class="col-md-2 column">
        </div>
    </div>
    <!--copyright版权说明-->
    <div class="container-fluid">
        <div class="row clearfix">
            <!--        <div class="col-md-1 column">-->
            <!--        </div>-->
            <!--        <div class="col-md-12 column">-->
            <div id="copyRight-frame">
                <strong>Copyright © 2017-2200.Company name All rights reserved.</strong>
            </div>
            <!--        </div>-->
            <!--        <div class="col-md-1 column">-->
            <!--        </div>-->
        </div>
    </div>
</div>

<script id="theTmpl" type="text/x-jsrender">
<input class="tdel" type="checkbox" name="checkbox" value="goods1"/>
<div class="">
    <div class="row clearfix">

        <div class="col-md-2 column" style="margin-left:-6%">
            <img src="{{:images}}" alt="" style="width: 120px; height: 120px; display: inline-block; vertical-align: bottom;">
        </div>
        <div class="col-md-2 column">
            <div style="display: inline-block; margin-left:22%">
                <p>{{:pname}}</p>
                <p class="getid" style="display:none;">{{:cid}}</p>
                <p style="color: #c4c4c4; font-size: 13px">分类：</p>
                <p style="color: #c4c4c4; font-size: 13px">{{:sort}}</p>
            </div>
        </div>
        <div class="col-md-2 column">
            <p style="display: inline-block; margin-left: 25%;">￥{{:price}}</p>
        </div>
        <div class="col-md-2 column" style="margin-left:3%">
            <div style="display: inline-block; margin-left: 11.5%">
                <span class="sub tdel" id="sub1" onclick="subNum();"><img src="img/sub.jpg" alt="" ></span>&nbsp;<span><input type="text" value="{{:count}}" class="val1 tdel-input" style="outline: none;"></span>
                <span class="add tdel" id="add1" onclick="addNum();"><img src="img/add.jpg" alt="" ></span>
            </div>
        </div>
        <div class="col-md-2 column">
            <div style="display: inline-block; margin-left: 30%" class="price">
            </div>
        </div>
        <div class="col-md-2 column" style="margin-left:3%">
            <p style="display: inline-block; margin-left: 13%;" class="del tdel">删除</p>
        </div>
    </div>
</div>
</script>

<script>
    var sum = [];
    var sum1 = [];
    // alert(data.data[0].count);
    var template = $.templates("#theTmpl");
    $.get("http://localhost:8080/ycumall/CartServlet?method=query", function (data) {
        var len = data.data.length;
        for(var i=0;i<len;i++){
            var htmlOutput = template.render(data.data[i]);
            $("#cart-div"+i).html(htmlOutput);
        }
        var price = document.getElementsByClassName("price");
        var count = document.getElementsByClassName("val1");
        for(var i = 0; i < price.length; i++){
            sum[i] = data.data[i].price;
            sum1[i] = data.data[i].price;
            price[i].innerHTML = data.data[i].price * count[i].value;
        }
        //减少商品
        var sub = document.getElementsByClassName("sub");
        for (var j = 0; j < sub.length; j++) {
            var a = sub[j];
            a.index = j;//给每个className为child的元素添加index属性;
            a.onclick = function () {
                // alert(this.index);
                if($(".val1")[this.index].value <= 1){
                    $(".val1")[this.index].value = 1;
                }else{
                    $(".val1")[this.index].value--;
                    // alert(this.index);
                    // sum[this.index] -= sum1[this.index];
                    // sum[this.index] = sum[this.index];
                    price[this.index].innerHTML = data.data[this.index].price * count[this.index].value;
                    // price[this.index] -= price1[this.index];
                    // jg[this.index].innerHTML = price[this.index];
                }
            }
        }
        //增加商品
        var add = document.getElementsByClassName("add");
        for (var i = 0; i < add.length; i++) {
            var b = add[i];
            b.index = i;//给每个className为child的元素添加index属性;
            b.onclick = function () {
                // alert(this.index);
                if($(".val1")[this.index].value >= 5){
                    alert("限购5件！");
                }else{
                    $(".val1")[this.index].value++;
                    sum[this.index] += sum1[this.index];
                    sum[this.index] = sum[this.index];
                    price[this.index].innerHTML = sum[this.index];
                    // price[this.index] += price1[this.index];
                    // jg[this.index].innerHTML = price[this.index];
                }
            }
        }
        //删除商品
        var del = document.getElementsByClassName("del");
        // alert(del.length);
        var cart_div = document.getElementsByClassName("cart-div");
        // alert(cart_div.length);
        for (var i = 0; i < del.length; i++) {
            var a = del[i];
            a.index = i;//给每个className为child的元素添加index属性;
            // alert(this.index);
            a.onclick = function () {
                // alert(this.index);
                //  del[this.index].click(function () {
                //     // alert(this.index);
                //      alert("123");
                //  })
                var cid = cart_div[this.index].getElementsByClassName("getid")[0].innerHTML;
                $.get("http://localhost:8080/ycumall/CartServlet?method=del", "cid="+cid, function (data) {
                    if (data.status === 200) {
                        alert("删除成功");
                    }
                });
                cart_div[this.index].remove();
            }
        }


    });
    function subNum(){

    }
    function addNum(){

    }
    $.get("http://localhost:8080/ycumall/UserServlet?method=getInfo", function (data) {
        if (data.data != null) {
            $("#login").html('<a href="#" style="color: black;font-size: 16px;">' + data.data + '</a>');
            $("#register").html('<a href="http://localhost:8080/ycumall/UserServlet?method=outLogin" style="color: black;font-size: 16px;">登出</a>');
            console.log(data.data);
        }
    });
    $.get("http://localhost:8080/ycumall/UserServlet?method=getInfo", function (data) {
        if (data.data != null) {
            $("#login").html('<a href="#" style="color: black;font-size: 16px;">' + data.data + '</a>');
            $("#register").html('<a href="http://localhost:8080/ycumall/UserServlet?method=outLogin" style="color: black;font-size: 16px;">登出</a>');
            console.log(data.data);
        }
    });
    function addOrder() {
        alert("提交成功！");
        $(".getid").each(function () {
            var cid = $(this).html();
            $.get("http://localhost:8080/ycumall/CartServlet?method=del", "cid="+cid, function (data) {
                if (data.status === 200) {
                    //
                }
            });
        })
        $(".cart-div").each(function () {
            // $(this).text("")
            $('.tdel').css('display', 'none');
            $(".tdel-input").css('border', '0px');
            $("#tdel-dis").text('商品价格')
        })
        var price = parseInt(0)
        $('.price').each(function () {
            var tprice = $(this).html()
            // console.log(tprice)
            price += parseInt(tprice)
        })
        var ht = '<button type="button" class="btn btn-primary btn-lg" onclick="payFor()">订单总价：'+ price +'</button>'
        + '<button type="button" class="btn btn-danger btn-lg" onclick="payFor()">点击支付</button>'
        $('#tdel-show').html(ht)
    }

    function payFor() {
        alert("支付成功");
        window.location.href = 'Ymall.html';
    }

</script>
</body>
</html>